package com.ymatou.iapi.platformdiscounts.testcase;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.platformdiscounts.parameter.CalculateBean;
import com.ymatou.iapi.platformdiscounts.parameter.DiscountInfo;
import com.ymatou.iapi.platformdiscounts.parameter.UpdateusestatusBean;
import com.ymatou.iapi.platformdiscounts.service.CalculateCall;
import com.ymatou.iapi.platformdiscounts.service.UpdateusestatusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DiscountsCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.discount_info;
import com.ymttest.database.model.discount_qualification;
import com.ymttest.database.sqlwapper.discount_infoWapper;
import com.ymttest.database.sqlwapper.discount_qualificationWapper;
import com.ymttest.utils.TestDataManager;
/**
*查询优惠信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_updateusestatus{
	private static UpdateusestatusBean bean;
	private static UpdateusestatusCall call;
	private static DiscountsCallService callService=new  DiscountsCallService();
	private discount_qualificationWapper dqWapper=new discount_qualificationWapper();
	private SellerProductCallService sellerProuctService=new SellerProductCallService();
	private discount_infoWapper infoWapper=new discount_infoWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询优惠信息");
	}

	@Before
	public void caseUp() {
		bean=new UpdateusestatusBean();
		call=new UpdateusestatusCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateusestatus_001() {
		Logger.start(true,"正例-更新使用状态");
		try {
			Logger.comment("############计新增优惠资格开始#################");
			CalculateBean calculateBean=callService.setCalculate();
			CalculateCall calculateCall=callService.callCalculated(calculateBean);
			Thread.sleep(5000);
			Logger.comment("############计新增优惠资格结束#################");
			discount_info record=new discount_info();
			record.setUserId(calculateBean.getUserId());
			record.setProductId(calculateBean.getProducts().get(0).getProductId());
			discount_info info=infoWapper.selectTop1(record);

			bean.setDiscountId(info.getDiscountId());
			bean.setDiscountTotalAmount(info.getSingleDiscountAmount());
			bean.setMainOrderId(TestDataManager.getRandomIntNum(5));
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateusestatus_002() {
		Logger.start(true,"正例-更新使用状态-验证实际优惠金额小于最大优惠金额");
		try {
			CalculateBean calculateBean=callService.setCalculate();
			CalculateCall calculateCall=callService.callCalculated(calculateBean);
			Logger.comment("############计新增优惠资格结束#################");
			discount_info record=new discount_info();
			record.setUserId(calculateBean.getUserId());
			record.setProductId(calculateBean.getProducts().get(0).getProductId());
			discount_info info=infoWapper.selectTop1(record);

			bean.setDiscountId(info.getDiscountId());
			bean.setDiscountTotalAmount(info.getSingleDiscountAmount().subtract(new BigDecimal(1)));
			bean.setMainOrderId(TestDataManager.getRandomIntNum(5));
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_updateusestatus_003() {
		Logger.start(true,"正例-更新使用状态-验证实际优惠金额大于最大优惠金额");
		try {
			Logger.comment("############计新增优惠资格开始#################");
			CalculateBean calculateBean=callService.setCalculate();
			CalculateCall calculateCall=callService.callCalculated(calculateBean);
			Thread.sleep(5000);
			Logger.comment("############计新增优惠资格结束#################");
			discount_info record=new discount_info();
			record.setUserId(calculateBean.getUserId());
			record.setProductId(calculateBean.getProducts().get(0).getProductId());
			discount_info info=infoWapper.selectTop1(record);

			bean.setDiscountId(info.getDiscountId());
			bean.setDiscountTotalAmount(info.getSingleDiscountAmount().add(new BigDecimal(1)));
			bean.setMainOrderId(TestDataManager.getRandomIntNum(5));
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateusestatus_004() {
		Logger.start(false,"反例-更新使用状态-验证discountId为空");
		try {
			Logger.comment("############计新增优惠资格开始#################");
			CalculateBean calculateBean=callService.setCalculate();
			CalculateCall calculateCall=callService.callCalculated(calculateBean);
			Thread.sleep(5000);
			Logger.comment("############计新增优惠资格结束#################");

			DiscountInfo info=calculateCall.getCallData().getProductDiscounts().get(0);
			//bean.setDiscountId(info.getDiscountId());
			bean.setDiscountTotalAmount(info.getDiscountAmount());
			bean.setMainOrderId(TestDataManager.getRandomIntNum(5));
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("fail", call.getReturnData(), "验证返回fail");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateusestatus_005() {
		Logger.start(false,"反例-更新使用状态-验证discountTotalAmount为空");
		try {
			Logger.comment("############计新增优惠资格开始#################");
			CalculateBean calculateBean=callService.setCalculate();
			CalculateCall calculateCall=callService.callCalculated(calculateBean);
			Thread.sleep(5000);
			Logger.comment("############计新增优惠资格结束#################");
			discount_info record=new discount_info();
			record.setUserId(calculateBean.getUserId());
			record.setProductId(calculateBean.getProducts().get(0).getProductId());
			discount_info info=infoWapper.selectTop1(record);

			bean.setDiscountId(info.getDiscountId());
			//bean.setDiscountTotalAmount(info.getSingleDiscountAmount());
			bean.setMainOrderId(TestDataManager.getRandomIntNum(5));
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateusestatus_006() {
		Logger.start(false,"反例-更新使用状态-验证mainOrderId为空");
		try {
			Logger.comment("############计新增优惠资格开始#################");
			CalculateBean calculateBean=callService.setCalculate();
			CalculateCall calculateCall=callService.callCalculated(calculateBean);
			Thread.sleep(5000);
			Logger.comment("############计新增优惠资格结束#################");
			discount_info record=new discount_info();
			record.setUserId(calculateBean.getUserId());
			record.setProductId(calculateBean.getProducts().get(0).getProductId());
			discount_info info=infoWapper.selectTop1(record);

			bean.setDiscountId(info.getDiscountId());
			bean.setDiscountTotalAmount(info.getSingleDiscountAmount());
			//bean.setMainOrderId(TestDataManager.getRandomIntNum(5));
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("fail", call.getReturnData(), "验证返回fail");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn()
	{
		Logger.verifyEquals("ok", call.getReturnData(), "验证返回ok");
		//验证数据库
		discount_info record=new discount_info();
		record.setDiscountId(bean.getDiscountId());
		discount_info info=infoWapper.selectTop1(record);
		
		Logger.verifyEquals(info.getDiscountTotalAmount(), bean.getDiscountTotalAmount()==null?new BigDecimal("0.00"): bean.getDiscountTotalAmount(), "验证discount_info字段DiscountTotalAmount是否正确");
		Logger.verifyEquals(info.getMainOrderId(), bean.getMainOrderId(), "验证discount_info字段MainOrderId是否正确");
		Logger.verifyEquals(1,info.getStatus() ,"验证discount_info字段Status=1,表示已经使用");
	}
}